home *** CD-ROM | disk | FTP | other *** search
/ God is Closer Than You Think / God is Closer.iso / mac / assets / tweenManager.as < prev    next >
Text File  |  2005-01-20  |  5KB  |  197 lines

  1. ╘¬°/*
  2. class tweenManager for tweening prototypes
  3. version 1.0
  4. Ladislav Zigo,lacoz@inmail.sk
  5. */
  6. class tweenManager{
  7.     static private var tweenList:Array = new Array(); 
  8.     static private var tweenHolder:MovieClip = _root.createEmptyMovieClip("_th_",6789);     
  9.     static private var playing:Boolean = false;
  10.     static private var now:Number;
  11.     //  private methods 
  12.     static private function init():Void{
  13.         tweenHolder.onEnterFrame = update;
  14.         playing = true;
  15.         now = getTimer();
  16.     }
  17.     static private function deinit():Void{
  18.         playing = false;
  19.         delete tweenHolder.onEnterFrame;
  20.     }
  21.  
  22.     static private function update():Void{
  23.         // loop all tween objects
  24.         var i = tweenList.length;
  25.         while (i--){
  26.             var t = tweenList[i];
  27.             if(t.ts > now){
  28.                 //trace("wait");
  29.                 // wait, next iteration
  30.                 continue;
  31.                 
  32.             }
  33.             if(t.ts + t.d > now){        
  34.                 // compute value using equation function
  35.                 if(t.ctm == undefined){
  36.                     // compute primitive value
  37.                     t.mc[t.pp] = t.ef(now-t.ts,t.ps,t.ch,t.d,t.e1,t.e2);
  38.                 }else{
  39.                     // compute color transform matrix 
  40.                     // stm is starting transform matrix, 
  41.                     // ctm is change in start & destination matrix 
  42.                     // ttm is computed (temporary) transform matrix
  43.                     // c is color object
  44.                     var ttm = {};
  45.                     for(var j in t.ctm){
  46.                         ttm[j] = t.ef(now-t.ts,t.stm[j],t.ctm[j],t.d,t.e1,t.e2) 
  47.                     }
  48.                     t.c.setTransform(ttm);
  49.                 }
  50.                 
  51.             } else {
  52.                 // end , set up the property to end value;
  53.                 if(t.ctm == undefined){
  54.                     t.mc[t.pp] = t.ps + t.ch;
  55.                 } else {
  56.                     var ttm = {};    
  57.                     for(var j in t.ctm){
  58.                         ttm[j] = t.stm[j]+t.ctm[j]
  59.                     }
  60.                     t.c.setTransform(ttm);    
  61.                 }
  62.                 endTween(i);
  63.             }
  64.         }
  65.         // update timer 
  66.         now = getTimer();
  67.     }
  68.     static private function endTween(id:Number):Void{
  69.         //callback function
  70.         var et = tweenList[id];
  71.         if(et.cb != undefined){
  72.             et.cb.func.apply(et.cb.scope,et.cb.args);
  73.             
  74.             if(tweenList[id] != et){
  75.                 // there was callback function changed the tweenList
  76.                 // we must find again the id 
  77.                 var i = tweenList.length
  78.                 while(i--){
  79.                     if (tweenList[i] == et){
  80.                         id = i
  81.                         break;
  82.                     }
  83.                 }
  84.             } 
  85.         }
  86.         tweenList.splice(id,1);
  87.         //
  88.         if(tweenList.length==0){
  89.             // last tween removed, erase onenterframe function
  90.             deinit();
  91.         }
  92.         
  93.         
  94.     }
  95.     //  public methods 
  96.     static public function addTween(mc:MovieClip,props:Array,pEnd:Array,
  97.                 sec:Number,delay:Number,eqFunc:Function,
  98.                 callback:Object,extra1:Number,extra2:Number):Void{
  99.         if(!playing){
  100.             init();
  101.         }
  102.         //
  103.         for(var i in props){
  104.             if(props[i].substr(0,4)!="_ct_"){
  105.                 // there is no color transform prefix, use primitive value tween
  106.                 tweenList.unshift({                            
  107.                           mc: mc,                // reference to movieclip        
  108.                           pp: props[i],         // property
  109.                           ps: mc[props[i]],        // starting value of property    
  110.                           ch: pEnd[i] - mc[props[i]],     // difference between starting and end value
  111.                           ts: now + delay * 1000,     // start time of tween
  112.                           d:  sec * 1000,         // duration of tween
  113.                           ef: eqFunc,             // reference to easing equation function
  114.                           cb: callback,            // callback object (function which is called at end of tween)
  115.                           e1: extra1,            // extra 1 value (for elastic and bouce equation)
  116.                           e2: extra2});            // extra 2 value (for elastic equation)
  117.             }else{
  118.                 // color trasform prefix found    
  119.                 // compute change matrix
  120.                 var c = new Color(mc);
  121.                 var stm = c.getTransform();
  122.                 // compute difference between starting and desionation matrix
  123.                 var ctm = {}
  124.                 for(var j in pEnd[i]){
  125.                     // if is in destination matrix 
  126.                     if(pEnd[i][j] != stm[j] ){
  127.                         ctm [j] = pEnd[i][j] - stm[j]
  128.                     }
  129.                 }
  130.                 // if there is no difference between start & end do not perform tween
  131.                 //if()
  132.                 tweenList.unshift({
  133.                         mc:  mc,            //reference to movieclip
  134.                         c:   c,                //reference to movieclip color
  135.                         stm: stm,            //starting transform matrix
  136.                         ctm: ctm,            
  137.                         ts:  now + delay * 1000,
  138.                         d:   sec * 1000,
  139.                         ef:  eqFunc,
  140.                         cb:  callback,
  141.                         e1:  extra1,
  142.                         e2:  extra2
  143.                 })            
  144.             }
  145.         }
  146.     }
  147.     
  148.     static public function removeTween(mc:MovieClip,props:Array):Void{
  149.         var all = false;
  150.         if(props == undefined){
  151.             // props are undefined, remove all tweens
  152.             var all = true;
  153.         }
  154.         var i = tweenList.length; 
  155.         while (i--){
  156.             if(tweenList[i].mc == mc){
  157.                 if(all){
  158.                     tweenList.splice(i,1);
  159.                 }else{
  160.                     for(var j in props){
  161.                         if(tweenList[i].pp == props[j]){
  162.                             tweenList.splice(i,1);
  163.                             // props.splice(j,1) 
  164.                             // (because allows add same properties for same mc,
  165.                             // all tweens must be checked) 
  166.                         }
  167.                     }
  168.                 }
  169.             }
  170.         }
  171.         if(tweenList.length==0){
  172.             // last tween removed, erase onenterframe function
  173.             deinit();
  174.         }
  175.     }
  176.     static public function isTweening(mc:MovieClip):Boolean{
  177.         var is = false;
  178.         for (var i in tweenList){
  179.             if(tweenList[i].mc == mc){
  180.                 // mc found, so break loop
  181.                 is = true;
  182.                 break;
  183.             }
  184.         }
  185.         return is;
  186.     }
  187.     static public function getTweens(mc:MovieClip):Number{
  188.         var count = 0;
  189.         for (var i in tweenList){
  190.             if(tweenList[i].mc == mc){
  191.                 // found, increase count
  192.                 count++;
  193.             }
  194.         }
  195.         return count;
  196.     }
  197. }